#include <iostream>
#include <sstream>
#include <SFML/Graphics/RenderWindow.hpp>
#include <nice2d/macros.h>
#include <nice2d/scene.h>
#include <nice2d/spritecontroller.h>
#include <nice2d/background.h>
#include <nice2d/imagedescriptor.h>
#include <nice2d/components/group.h>
#include <nice2d/components/spritestatus.h>
#include <nice2d/components/imagemanager.h>
#include <nice2d/components/soundmanager.h>
#include <nice2d/widgets/desktop.h>
#include <nice2d/widgets/boxlayout.h>
#include <nice2d/widgets/button.h>
#include <nice2d/util.h>

#define SCENE_WIDTH  1152.0f
#define SCENE_HEIGHT 720.0f

#define OFFSET 10

class UnitScene : public Scene, public SpriteController
{
public:
    void onInitialed()override
    {
        setSceneBox({0.0f, 0.0f, SCENE_WIDTH, SCENE_HEIGHT});
        setBackground(Background::create("./../data/images/background.png", true));

        for (int i = 0; i < 60; i++) {
            auto sprite = createSprite(rand() % int(SCENE_WIDTH), rand() % int(SCENE_HEIGHT));
            //sprite->setRotate(-180.0f);
            GET_COMPONENT(sprite, Group)->setGroup(0);
            sprite->setSpriteColor(sf::Color(rand() % 255, rand() % 255, rand() % 255));
            addChild(sprite);

            sprite->setSpriteController(as<SpriteController>());
        }
    }

    void setScene(SceneWeakPointer scene) override
    {
        (void)scene;
    }

    std::shared_ptr<Sprite> createSprite(float x, float y)
    {
        auto sprite = std::make_shared<Sprite>();
        //sprite->setScale(0.5f);
        sprite->setPosition(x, y);
        GET_COMPONENT(sprite, SpriteStatus)->setSpriteStatus(SPRITE_STATUS_NORMAL);

        sprite->addSpriteImage("./../data/images/plane.png");
        return sprite;
    }

    void updateSprite(SpritePointer sprite, float deltaTime) override
    {
        sprite->move(-96.0f * deltaTime);
        sf::FloatRect sceneBox = getSceneBox();

        auto spritePosition = sprite->getPosition();
        if (spritePosition.y < OFFSET)
            sprite->setPosition(spritePosition.x, sceneBox.height + OFFSET);
    }
};

DECLARE_MAIN_FUNCTION_WITH_SCENE(SCENE_WIDTH, SCENE_HEIGHT, "unit12", UnitScene)
